package servlets;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dataBase.magaers.CodeDBMngr;

/**
 * Servlet implementation class ShowCodeServlet
 */
public class ShowCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowCodeServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("cid");
		String version = request.getParameter("version");
		if ((cid == null) || (version == null)){
			response.sendError(400);
			return;
		}
		CodeDBMngr cdbm = CodeDBMngr.getInstance();
		if (cdbm == null){
			response.sendError(500);
			return;
		}
		String body = cdbm.searchCodeByCidVersion(cid, version);
		if (body == null){
			response.sendError(500);
			return;
		}
		ServletOutputStream out = response.getOutputStream();
		String htmlToShow = "<html><head><title>Code Displaying</title></head>" +
				"<body><p><a href=\"site/xslt.jsp\">new xslt</a></p><br />" +
				"<form action=\"\"><table><tr><td><input type=\"submit\" name=\"edit\" value=\"edit\" /></td>" +
				"<td><input type=\"submit\" name=\"delete\" value=\"delete\" /></td>" +
				"<td><input type=\"checkbox\" name=\"visible\" value=\"visible\" /></td>" +
				"<hr />" + body +
				"<p><a href=\"site/search.html\">new search</a>" +
				"</body></html>";
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendError(404);
		return;		
	}

}
